//
//  ZWNetworkManager.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/19.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
import AFNetworking

// 请求方式
enum ZWRequestMethod: String {
    case Post = "post"
    case Get = "get"
}


class ZWNetworkTools: AFHTTPSessionManager {
    
    // 单例：全局访问点
    static let sharedTools: ZWNetworkTools = {
        let tools = ZWNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    // 定义请求
    typealias ZWRequestCallBack = (responseObject: AnyObject?, error: NSError?)->()
    
    
    func request(method: ZWRequestMethod = .Get, urlString: String, parameters: AnyObject?, finished: ZWRequestCallBack){
        
        // 定义一个请求成功之后要执行的闭包
        let success = { (dataTask: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
            // 请求成功的回调
            finished(responseObject: responseObject, error: nil)
        }
        
        // 定义一个请求失败之后要执行的闭包
        let failure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(responseObject: nil, error: error)
        }
        
        if method == .Get {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    
    
}
